package cn.edu.zut.soft.student.service.student.impl;

import cn.edu.zut.soft.student.bean.PageBean;
import cn.edu.zut.soft.student.dao.ClassMapper;
import cn.edu.zut.soft.student.dao.StudentMapper;
import cn.edu.zut.soft.student.model.Class;
import cn.edu.zut.soft.student.model.Course;
import cn.edu.zut.soft.student.model.Student;
import cn.edu.zut.soft.student.model.StudentExample;
import cn.edu.zut.soft.student.service.class_.ClassBo;
import cn.edu.zut.soft.student.service.student.StudentBo;
import cn.edu.zut.soft.student.service.student.StudentSelectCoursesBo;
import cn.edu.zut.soft.student.service.student.vo.StudnetQuery;
import cn.edu.zut.soft.student.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/22 14:53
 */

@Service
public class StudentBoImpl implements StudentBo {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private ClassBo classBo;
    @Autowired
    private StudentSelectCoursesBo sscb;

    @Override
    public int addStudent(Student student) {
        Class aClass = classMapper.selectByPrimaryKey(student.getStudentclassid());
        aClass.setAmount(aClass.getAmount() + 1);
        classBo.updateClass(aClass);
        return studentMapper.insert(student);
    }

    @Override
    public int importStudents(MultipartFile file) throws Exception {
        List<Student> students = ExcelUtil.getStudentsFromExcel(file.getInputStream());
        students.forEach(student -> addStudent(student));
        return 0;
    }

    @Override
    public int deleteStudent(String studentid) {
        List<Course> courses = sscb.querySelectCourses(studentid);
        courses.forEach(course -> deleteSelectCourse(course.getCourseid(), studentid));
        Class aClass = classMapper.selectByPrimaryKey(studentMapper.selectByPrimaryKey(studentid).getStudentclassid());
        aClass.setAmount(aClass.getAmount() - 1);
        classBo.updateClass(aClass);
        return studentMapper.deleteByPrimaryKey(studentid);
    }

    @Override
    public int updateStudent(Student student) {
        Student oldstudent = studentMapper.selectByPrimaryKey(student.getStudentid());
        if (!oldstudent.getStudentclassid().equals(student.getStudentclassid())) {
            Class oldClass = classMapper.selectByPrimaryKey(oldstudent.getStudentclassid());
            oldClass.setAmount(oldClass.getAmount() - 1);
            classBo.updateClass(oldClass);

            Class newClass = classMapper.selectByPrimaryKey(student.getStudentclassid());
            newClass.setAmount(newClass.getAmount() + 1);
            classBo.updateClass(newClass);
        }
        return studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public Student queryStudentById(String studentid) {
        return studentMapper.selectByPrimaryKey(studentid);
    }

    @Override
    public PageBean<List<Student>> queryStudents(StudnetQuery studnetQuery) {
        StudentExample studentExample = new StudentExample();
        studentExample.setOffset(studnetQuery.getOffset());
        studentExample.setLimit(studnetQuery.getLimit());
        StudentExample.Criteria criteria = studentExample.createCriteria();
        if (studnetQuery.getStudentid() != null && !"-1".equals(studnetQuery.getStudentid())) {
            criteria.andStudentidLike("%" + studnetQuery.getStudentid() + "%");
        }
        if (studnetQuery.getStudentname() != null && !"-1".equals(studnetQuery.getStudentname())) {
            criteria.andStudentnameLike("%" + studnetQuery.getStudentname() + "%");
        }
        List<Student> students = studentMapper.selectByExample(studentExample);
        students.forEach(student -> student.setStudentclassid(classMapper.selectByPrimaryKey(student.getStudentclassid()).getClassname()));
        return new PageBean<>(students, studentMapper.countByExample(studentExample));
    }

    @Override
    public int resetPassword(String studentid, String password) {
        return studentMapper.updatePassword(password, studentid);
    }

    @Override
    public int selectCourse(String courseid, String studentid) {
        return studentMapper.selectCourse(courseid, studentid);
    }

    @Override
    public int deleteSelectCourse(String courseid, String studentid) {
        return studentMapper.deleteSelectCourse(courseid, studentid);
    }
}
